package com.easipass.framework.eirframeworkcoredsl;

import java.util.Collections;
import java.util.List;

public class FlowNode extends LabelledServiceNode {

    private List<LabelledServiceNode> series;

    FlowNode(List<LabelledServiceNode> nodes) {
        super(nodes.get(0).getStartPos(), nodes.get(nodes.size() - 1).getEndPos());
        this.series = Collections.unmodifiableList(nodes);
    }

    @Override
    public String stringify(boolean includePositionInfo) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < series.size(); i++) {
            if (i > 0) {
                s.append(" ").append(TokenKind.ANDAND.tokenChars).append(" ");
            }
            s.append(series.get(i).stringify(includePositionInfo));
        }
        return s.toString();
    }

    @Override
    public int getSeriesLength() {
        return series.size();
    }

    @Override
    public List<LabelledServiceNode> getSeries() {
        return series;
    }

    @Override
    public LabelledServiceNode getSeriesElement(int index) {
        return series.get(index);
    }

    @Override
    public boolean isFlow() {
        return true;
    }

    @Override
    public String toString() {
        return "[Flow:" + stringify(true) + "]";
    }

    @Override
    public void accept(Visitor visitor) {
        boolean cont = visitor.preVisit(this);
        if (!cont) {
            return;
        }
        visitor.visit(this);
        for (LabelledServiceNode labelledServiceNode : series) {
            labelledServiceNode.accept(visitor);
        }
        visitor.postVisit(this);
    }
}
